import requests
from jsonpath import jsonpath
import sys
from openpyxl import workbook   # 调出excel

wb = workbook.Workbook()   # 创建excel对象
ws = wb.active   # 激活表对象
ws.append(['标题', '链接', '地点'])


def get_data():
    try:
        r = requests.get(url, headers=headers, params=url_2)
        if r.status_code == 200:
            print(r.status_code)
            json_data = r.json()
            return json_data

    except:
        # 设置递归限制
        return get_data()

def jiexi_data(json_data):
    try:
        # $ 表示根节点   ..表示跳过任意层级
        new_name= jsonpath(json_data,"$..RecruitPostName")
        new_url = jsonpath(json_data,"$..PostURL")
        new_media = jsonpath(json_data,"$..LocationName")

        for title,news,media in zip(new_name,new_url,new_media):
            print(title)
            print(news)
            print(media)
            print("="*10)
            save_data(title,news,media)


    except:
        sys.exit(1) # 终止程序 0为正常推出  0-127为不正常推出




# 保存为excel
def save_data(tit, link, name):
    my_list = [tit,link,name]   # 以列表形式写入数据
    ws.append(my_list)
    wb.save("腾讯社会.xlsx")   # 保存

if __name__ == '__main__':

    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'

    }
    for i in range(10):
        url = "https://careers.tencent.com/tencentcareer/api/post/Query"
        url_2={
            'timestamp': '1645520413160',
            'countryId':'',
            'cityId':'',
            'bgIds':'',
            'productId':'',
            'categoryId':'',
            'parentCategoryId':'',
            'attrId':'',
            'keyword':'',
            'pageIndex': '{}'.format(i + 1),
            'pageSize': '10',
            'language': 'zh-cn',
            'area': 'cn',
            }
        a = get_data()

        jiexi_data(a)













































